package com.sykj.cloud.log.controller;

import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.web.page.TableDataInfo;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.log.service.ILogUserService;
import com.sykj.cloud.system.api.domain.LogUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户日志Controller
 *
 * @author mr.will
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/user")
public class LogUserController extends BaseController
{
    @Autowired
    private ILogUserService logUserService;


    /**
     * 新增用户日志
     */
    @PostMapping
    public AjaxResult add(@RequestBody LogUser logUser)
    {
        return toAjax(logUserService.insertLogUser(logUser));
    }



    /**
     * 删除用户日志
     */
    @PreAuthorize(hasPermi = "log:user:remove")
    @Log(title = "用户日志", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(logUserService.deleteLogUserByIds(guids));
    }

    /**
     * 获取用户日志详细信息
     */
    @PreAuthorize(hasPermi = "log:user:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(logUserService.getLogUserById(guid));
    }

    /**
     * 导出用户日志列表
     */
    @PreAuthorize(hasPermi = "log:user:export")
    @Log(title = "用户日志", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LogUser logUser) throws IOException
    {
        List<LogUser> list = logUserService.listLogUsers(logUser);
        ExcelUtil<LogUser> util = new ExcelUtil<LogUser>(LogUser.class);
        util.exportExcel(response, list, "user");
    }


    /**
     * 查询用户日志列表
     */
    @PreAuthorize(hasPermi = "log:user:list")
    @GetMapping("/list")
    public TableDataInfo list(LogUser logUser)
    {
        startPage();
        List<LogUser> list = logUserService.listLogUsers(logUser);
        return getDataTable(list);
    }
    }
